home *** CD-ROM | disk | FTP | other *** search
- /************************************************************/
- /* */
- /* cdev Code from Chapter Three of */
- /* */
- /* *** The Macintosh Programming Primer *** */
- /* */
- /* Copyright 1990, Dave Mark */
- /* */
- /* This program demonstrates specific Mac programming */
- /* techniques. */
- /* */
- /************************************************************/
-
- #define DEFAULT_ITEM 1
- #define USER_ITEM 2
-
- #define RUN_ON_ALL_MACHINES 1L
- #define ERROR_STATE 0L
- #define WORD_RES_ID -4048
- #define FONT_MENU_ID -4048
-
- #define NORMAL_APP_FONT applFont
-
- short FindFontNumber();
-
- typedef struct
- {
- short curFontNum;
- } FontNumInfo, **FontNumH;
-
-
- pascal long main( message, item, numItems, cPanelID, e, cDevValue, cpDialog )
- int message, item, numItems, cPanelID;
- EventRecord *e;
- long cDevValue;
- DialogPtr cpDialog;
- {
- int itemType, fontNumber, choice;
- Handle itemH, tempHandle;
- Rect itemRect;
- MenuHandle fontMenu;
- Str255 tempStr;
-
- if ( message == macDev )
- return( RUN_ON_ALL_MACHINES );
- else if ( message == initDev )
- {
- tempHandle = NewHandle( sizeof( FontNumInfo ) );
- fontNumber = (short)FindFontNumber();
- (**((FontNumH)tempHandle)).curFontNum = fontNumber;
- return( (long)tempHandle );
- }
-
- if ( (cDevValue != cdevUnset) && (cDevValue != ERROR_STATE) )
- {
- switch( message )
- {
- case hitDev:
- if ( item == DEFAULT_ITEM + numItems )
- {
- GetDItem( cpDialog, USER_ITEM + numItems, &itemType, &itemH, &itemRect );
- fontNumber = NORMAL_APP_FONT;
- SetAppFont( fontNumber );
- DrawFontName( fontNumber, &itemRect );
- (**((FontNumH)cDevValue)).curFontNum = fontNumber;
- FixResource( fontNumber );
- }
- else if ( item == USER_ITEM + numItems )
- {
- GetDItem( cpDialog, USER_ITEM + numItems, &itemType, &itemH, &itemRect );
- fontMenu = GetMenu( FONT_MENU_ID );
- InsertMenu( fontMenu, -1 );
- AddResMenu( fontMenu, 'FONT' );
- itemRect.right += 1;
- choice = DoPopup( &itemRect, fontMenu );
-
- if ( choice != 0 )
- {
- GetItem( fontMenu, choice, &tempStr );
- GetFNum( tempStr, &fontNumber );
- SetAppFont( fontNumber );
- DrawFontName( fontNumber, &itemRect );
- (**((FontNumH)cDevValue)).curFontNum = fontNumber;
- FixResource( fontNumber );
- }
-
- DeleteMenu( FONT_MENU_ID );
- ReleaseResource( fontMenu );
- }
- break;
- case closeDev:
- DisposHandle( (Handle)cDevValue );
- break;
- case nulDev:
- break;
- case updateDev:
- GetDItem( cpDialog, USER_ITEM+numItems, &itemType, &itemH, &itemRect );
- FrameRect( &itemRect );
- MoveTo( itemRect.left + 1, itemRect.bottom );
- LineTo( itemRect.right, itemRect.bottom );
- LineTo( itemRect.right, itemRect.top + 1 );
- fontNumber = (**((FontNumH)cDevValue)).curFontNum;
- DrawFontName( fontNumber, &itemRect );
- break;
- case activDev:
- break;
- case deactivDev:
- break;
- case keyEvtDev:
- break;
- case macDev:
- return( 1L );
- break;
- case undoDev:
- break;
- case cutDev:
- break;
- case copyDev:
- break;
- case pasteDev:
- break;
- case clearDev:
- break;
- }
- }
-
- return( cDevValue );
- }
-
-
- /******************************** FixResource *******/
-
- FixResource( fontNumber )
- short fontNumber;
- {
- Handle wHandle;
-
- if ( ( wHandle = GetResource( 'word', WORD_RES_ID ) ) != 0L )
- {
- *( (short *)(*wHandle) ) = fontNumber;
- ChangedResource( wHandle );
- WriteResource( wHandle );
- }
- }
-
-
- /******************************** DoPopup *******/
-
- int DoPopup( popupRectPtr, theMenu )
- Rect *popupRectPtr;
- MenuHandle theMenu;
- {
- Point popupUpperLeft;
- long theChoice = 0x0000;
-
- popupUpperLeft.h = popupRectPtr->left + 2;
- popupUpperLeft.v = popupRectPtr->bottom;
-
- LocalToGlobal( &popupUpperLeft );
-
- InvertRect( popupRectPtr );
- theChoice = PopUpMenuSelect( theMenu, popupUpperLeft.v, popupUpperLeft.h, 0 );
- InvertRect( popupRectPtr );
- return( LoWord( theChoice ) );
- }
-
-
- /********************************************** FindFontNumber */
-
- short FindFontNumber()
- {
- Handle wHandle;
- short fontNumber;
-
- if ( ( wHandle = GetResource( 'word', WORD_RES_ID ) ) != 0L )
- {
- fontNumber = *( (short *)(*wHandle) );
- return( fontNumber );
- }
- else
- return( NORMAL_APP_FONT );
- }
-
-
- /********************************************** SetAppFont */
-
- SetAppFont( fontNum )
- short fontNum;
- {
- *( (short *) 0x0204 ) = fontNum - 1;
-
- WriteParam();
- }
-
-
- /********************************************** DrawFontName */
-
- DrawFontName( fontNum, rPtr )
- short fontNum;
- Rect *rPtr;
- {
- Str255 tempStr;
- int w;
- Rect tempRect;
-
- tempRect = *rPtr;
- InsetRect( &tempRect, 2, 2 );
- EraseRect( &tempRect );
- if ( fontNum == 1 )
- GetFontName( geneva, &tempStr );
- else
- GetFontName( fontNum, &tempStr );
- w = rPtr->right - rPtr->left - StringWidth( tempStr );
- MoveTo( rPtr->left + w/2, rPtr->bottom - 4 );
- DrawString( tempStr );
- }